//
//  FPRecommonProductController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/4/22.
//

#import "FPRecommonProductController.h"
#import "FPRecommonProductCell.h"
#import "FPRepayPlanModel.h"

@interface FPRecommonProductController () <UITableViewDelegate , UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UILabel *tipLab1;
@property (weak, nonatomic) IBOutlet UILabel *tipLab2;
@property (weak, nonatomic) IBOutlet UIButton *mainBtn;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIView *tipLabBackView;

@end

@implementation FPRecommonProductController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initUI {
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    [self setTitle:@"借款详情" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    self.tipLab1.textColor = [UIColor hexColor:@"#6A6A6A"];
    self.tipLab2.textColor = MAIN_BLUE_COLOR;
    
    self.backView.clipsToBounds = YES;
    self.backView.layer.cornerRadius = 8.f;
    self.tipLabBackView.clipsToBounds = YES;
    self.tipLabBackView.layer.cornerRadius = 4.f;
    self.tipLabBackView.backgroundColor = [UIColor hexColor:@"#EAF2FF"];
    self.tipLab2.adjustsFontSizeToFitWidth = YES;
    
    self.contentView.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.contentView addSubview:self.tableView];
    self.tableView.rowHeight = 82.f;
    self.tableView.frame = CGRectMake(0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.safeDistanceTop - 64.f - 210.f - 8.f - 8.f - 44.f - UIDevice.safeDistanceBottom - 16.f);
    self.tableView.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    self.tableView.alwaysBounceVertical = NO;
    
    self.mainBtn.clipsToBounds = YES;
    self.mainBtn.layer.cornerRadius = 22.f;
    self.mainBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    [self.mainBtn setTitle:@"全部申请" forState:(UIControlStateNormal)];
//    [self.mainBtn setTitle:@"回到首页" forState:(UIControlStateNormal)];
    
    
    
    // 删除控制器
    NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    
    for (UIViewController *vc in marr) {
    
        if ([vc isKindOfClass:NSClassFromString(@"FPLoanController")]) {
            
            [marr removeObject:vc];
            break;
        }
    }
    
    self.navigationController.viewControllers = marr;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.productList.count >= 2 ? 1 : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPRecommonProductCell *cell;
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPRecommonProductCell.class) owner:nil options:nil].lastObject;
    }
    
    cell.applyBtn.indexPath = indexPath;
    
    if (self.productList.count >= 2) {
        
        FPProducts *product = self.productList.lastObject;
        [cell.logoImgView sd_setImageWithURL:[NSURL URLWithString:product.productLogo] placeholderImage:IMG(@"icon_home_placeholder")];
        cell.titleLab.text = product.productName;
        cell.subTitleLab.text = product.companyName;
        
        if (product.isSelect) {
            
            [cell.applyBtn setTitle:@"已申请" forState:(UIControlStateNormal)];
            cell.applyBtn.enabled = NO;
        }
        
        [cell.applyBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
            
            
            
            FPProducts *product = self.productList.lastObject;
            NSString *num = [FPUserModel AES256Encrypt:FPUserInfoModel.sharedUserInfo.phone];
            NSMutableDictionary *param = APIClient.creatAPIDictionary;
            
            [param setValue:product.advertId            forKey:@"advertId"];
            [param setValue:product.companyId           forKey:@"companyId"];
            [param setValue:self.infoModel.customerId   forKey:@"customerId"];
            [param setValue:self.infoModel.requestId    forKey:@"requestId"];
            [param setValue:num                         forKey:@"customerPhone"];
            [param setValue:product.price               forKey:@"price"];
            [param setValue:product.productName         forKey:@"productName"];
            [param setValue:product.productLogo         forKey:@"productLogo"];
            
            [param setValue:product.cooperateName       forKey:@"cooperateName"];
            [param setValue:product.companyName         forKey:@"companyName"];
            
            [MBManager showLoading];
            [FPRepayPlanModel applyLoanWithParam:param success:^(NSInteger code, NSString * _Nonnull message , BOOL isSuccess) {
                [MBManager hideLoading];
                
                if (code == 200) {
                    
//                    [self showText:@"申请成功"];
//                    [self performSelector:@selector(backAction) withObject:nil afterDelay:1.5f];
                    [[NSNotificationCenter defaultCenter] postNotificationName:LoanApplySuccessNotification object:nil];
                    
                    product.isSelect = YES;
                    [self.tableView reloadData];
                }
                else {
                    
                    [self showText:message];
                }
                
            } failure:^(NSError * _Nonnull error) {
                [self showError:error];
            }];
            
            // 埋点
            [FPRecordPointModel recordPointWithType:@"4" success:^(NSInteger code, NSString * _Nonnull message) {
            } failure:^(NSError * _Nonnull error) {
            }];
        }];
    }
    
    return cell;
}

    


@end
